<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>事件列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <el-row :gutter="20">
                <el-col :span="4">
                    <el-button type="primary" @click="showAddDialog">添加事件</el-button>
                </el-col>
            </el-row>
             <!-- 表格 -->
             <el-table :data="eventList"  stripe >
                <el-table-column type="index"></el-table-column>
                <el-table-column label="事件" prop="event" :filters="eventFilters" :filter-method="filterHandler"></el-table-column>
                <!-- <el-table-column label="关联状态" prop="hasChild" :filters="isConnetedFilters" :filter-method="filterHandler"></el-table-column> --> -->
                <el-table-column label="下级关联状态">
                    <!-- 作用域插槽 -->
                    <template slot-scope="scope">
                        <!-- @change="stateChanged(scope.row)" -->
                        <el-switch v-model="scope.row.hasChild" disabled></el-switch>
                    </template> 
                </el-table-column>

                <el-table-column label="操作">
                     <!-- 作用域插槽  在其中放置一些button-->
                    <template slot-scope="scope">
                        <!-- <el-button type="primary" size="mini" plain @click="showDetail(scope.row.id)">详情</el-button> -->
                        <el-button type="warning" size="mini" plain @click="showEditDialog(scope.row.id)">修改</el-button>
                        <el-button type="danger" size="mini" plain @click="deleteEventById(scope.row.id)">删除</el-button>
                    </template> 
                </el-table-column>

            </el-table>
        </el-card>
        <!-- 添加对话框 -->
        <el-dialog
            title="添加事件"
            :visible.sync="addDialogVisible"
            width="50%"
            @close="addDialogClosed">
            <!-- 对话框内容主体 -->
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                <el-form-item label="事件名称" prop="event"> <!--prop 是在editFormRule中用的-->
                    <el-input v-model="addForm.event"></el-input>
                </el-form-item>
                <el-form-item label="时间" prop="time">
                    <el-select v-model="addForm.timeId" placeholder="请选择时间">
                        <el-option v-for="item in timeOptions" :key="item.id" :value="item.id" :label="item.time"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="地点" prop="address">
                    <el-select v-model="addForm.addressIdList" multiple placeholder="请选择地点">
                        <el-option v-for="item in addressOptions" :key="item.id" :label="item.address" :value="item.id"> </el-option>  
                    </el-select>
                </el-form-item>
                <el-form-item label="知识点" prop="knowledgeElement">
                    <el-select v-model="addForm.knowledgeElementIdList" multiple  placeholder="请选择知识点">
                        <el-option v-for="item in knowledgeElementOptions" :key="item.id" :value="item.id" :label="item.content"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="人物" prop="people">
                    <el-select v-model="addForm.peopleIdList" multiple placeholder="请选择人物">
                        <el-option v-for="item in peopleOptions" :key="item.id" :value="item.id" :label="item.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="地图" prop="map">
                    <el-select v-model="addForm.mapId" placeholder="请选择地图">
                        <el-option v-for="item in mapOptions" :key="item.id" :value="item.id" :label="item.description"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <!-- 对话框底部 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addEventInfo">确 定</el-button>
            </span>
        </el-dialog>         
        <!-- 修改对话框 -->
        <el-dialog
            title="修改事件"
            :visible.sync="editDialogVisible"
            width="50%"
            @close="editDialogClosed">
            <!-- 对话框内容主体 -->
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
                <el-form-item label="事件名称" prop="event"> <!--prop 是在editFormRule中用的-->
                    <el-input v-model="editForm.event"></el-input>
                </el-form-item>
                <el-form-item label="时间" prop="time">
                    <el-select v-model="editForm.timeId" placeholder="请选择时间">
                        <el-option v-for="item in timeOptions" :key="item.id" :value="item.id" :label="item.time"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="地点" prop="address">
                    <el-select v-model="editForm.addressIdList" multiple placeholder="请选择地点">
                        <el-option v-for="item in addressOptions" :key="item.id" :label="item.address" :value="item.id"> </el-option>  
                    </el-select>
                </el-form-item>
                <el-form-item label="知识点" prop="knowledgeElement">
                    <el-select v-model="editForm.knowledgeElementIdList" multiple  placeholder="请选择知识点">
                        <el-option v-for="item in knowledgeElementOptions" :key="item.id" :value="item.id" :label="item.content"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="人物" prop="people">
                    <el-select v-model="editForm.peopleIdList" multiple placeholder="请选择人物">
                        <el-option v-for="item in peopleOptions" :key="item.id" :value="item.id" :label="item.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="地图" prop="map">
                    <el-select v-model="editForm.mapId" placeholder="请选择地图">
                        <el-option v-for="item in mapOptions" :key="item.id" :value="item.id" :label="item.description"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <!-- 对话框底部 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editEventInfo">确 定</el-button>
            </span>
        </el-dialog> 
    </div>
</template>

<script>
    export default {
        data() {
            return {
                // 查询list参数
                queryInfo: {
                    pageNum:1,
                    pageSize:20,
                    query:''
                },
                // 时间下拉列表选项
                timeOptions:[
                    // {id:'58', time: '1818年5月5日'},
                    // {id:"76", time:"1843年10月-1845年2月"}
                ], 
                // 地址下拉列表选项
                addressOptions: [
                    // {id: '538',address: '德国特里尔'}, 
                    // {id: '555',address: '德国巴门'}
                ],
                // 知识点下拉列表
                knowledgeElementOptions: [
                    // {id:"305", content:"甘斯与理性法学派"},
                    // {id:"308", content:"《伍珀河谷来信》"}
                ],
                peopleOptions: [
                    // {id:"1", name:"马克思"}, 
                    // {id:"2", name:"恩格斯"},
                ],
                mapOptions: [
                    // {id:"3", description:"德国地图"}, 
                    // {id:"4", description:"欧洲地图"}
                ],
                editDialogVisible: false,
                addDialogVisible: false,
                // 事件列表总列表
                eventList: [
                    // {id: "172", event:"马克思诞生于德国特里尔市", hasChild:0},
                    // {id: "173", event:"恩格斯诞生于德国巴门市", hasChild:1}
                ],
                // 编辑对话框绑定数据
                editForm: {
                    event:"",
                    timeId:"",
                    addressIdList:[],  // 地址id 列表
                    knowledgeElementIdList:[],
                    peopleIdList:[],
                    mapId:""
                },
                addForm: {
                    event:"",
                    timeId:"",
                    addressIdList:[],  // 地址id 列表
                    knowledgeElementIdList:[],
                    peopleIdList:[],
                    mapId:""
                },
                // 添加表单的验证规则对象
                editFormRules:{
                    event:[
                        { required: true, message: '请输入事件', trigger: 'blur' },
                        { min: 2, max: 100, message: '长度在2~100个字符', trigger: 'blur' }
                    ]
                },
                addFormRules:{
                    event:[
                        { required: true, message: '请输入事件', trigger: 'blur' },
                        { min: 2, max: 100, message: '长度在2~100个字符', trigger: 'blur' }
                    ]
                },
                // 事件列表过滤内容
                eventFilters:[]
            }
        },
        created() {
            this.getEventList();
        },
        methods: {
            // 展示新增事件的对话框（修改）
            async showAddDialog(id) {
                this.addDialogVisible = true;
                this.timeOptions = [{id:null, time:"不选"}];
                this.addressOptions = [];
                this.peopleOptions = [];
                this.mapOptions = [{id:null, description:"不选"}];
                this.knowledgeElementOptions = [];

                const{data:resTime} = await this.$http.get("month/selectAll");
                const timeNodeList = resTime.data
                for (let i = 0; i < timeNodeList.length; i++) {
                    const curTime = timeNodeList[i];
                    this.timeOptions.push({id:curTime.id, time:curTime.year+"年"+curTime.month})
                }
                
                const{data:resAddress} = await this.$http.get("address/selectAll");
                const addressNodeList = resAddress.data
                for (let i = 0; i < addressNodeList.length; i++) {
                    const curAddress = addressNodeList[i];
                    this.addressOptions.push({id:curAddress.id, address:curAddress.address})
                }

                const{data:resPeople} = await this.$http.get("people/selectAll");
                const peopleNodeList = resPeople.data
                for (let i = 0; i < peopleNodeList.length; i++) {
                    const curPeople = peopleNodeList[i];
                    this.peopleOptions.push({id:curPeople.id, name:curPeople.name})
                }

                const{data:resMap} = await this.$http.get("fileNode/selectAll", {params:{scene:"map"}});
                const mapNodeList = resMap.data
                for (let i = 0; i < mapNodeList.length; i++) {
                    const curMap = mapNodeList[i];
                    this.mapOptions.push({id:curMap.id, description:curMap.description})
                }


                const{data:resKE} = await this.$http.get("knowledgeElement/selectAll");
                const keNodeList = resKE.data
                for (let i = 0; i < keNodeList.length; i++) {
                    const curKE = keNodeList[i];
                    this.knowledgeElementOptions.push({id:curKE.id, content:curKE.content})
                }

            },
            // 展示编辑事件的对话框（修改）
            async showEditDialog(id) {
                this.editDialogVisible = true;
                this.timeOptions = [{id:null, time:"不选"}];
                this.addressOptions = [];
                this.peopleOptions = [];
                this.mapOptions = [{id:null, description:"不选"}];
                this.knowledgeElementOptions = [];

                const{data:res} = await this.$http.get("event/selectOne", {params:{"id":id}});
                this.editForm = res.data;

                const{data:resTime} = await this.$http.get("month/selectAll");
                const timeNodeList = resTime.data
                for (let i = 0; i < timeNodeList.length; i++) {
                    const curTime = timeNodeList[i];
                    this.timeOptions.push({id:curTime.id, time:curTime.year+"年"+curTime.month})
                }
                
                const{data:resAddress} = await this.$http.get("address/selectAll");
                const addressNodeList = resAddress.data
                for (let i = 0; i < addressNodeList.length; i++) {
                    const curAddress = addressNodeList[i];
                    this.addressOptions.push({id:curAddress.id, address:curAddress.address})
                }

                const{data:resPeople} = await this.$http.get("people/selectAll");
                const peopleNodeList = resPeople.data
                for (let i = 0; i < peopleNodeList.length; i++) {
                    const curPeople = peopleNodeList[i];
                    this.peopleOptions.push({id:curPeople.id, name:curPeople.name})
                }

                const{data:resMap} = await this.$http.get("fileNode/selectAll", {params:{scene:"map"}});
                const mapNodeList = resMap.data
                for (let i = 0; i < mapNodeList.length; i++) {
                    const curMap = mapNodeList[i];
                    this.mapOptions.push({id:curMap.id, description:curMap.description})
                }


                const{data:resKE} = await this.$http.get("knowledgeElement/selectAll");
                const keNodeList = resKE.data
                for (let i = 0; i < keNodeList.length; i++) {
                    const curKE = keNodeList[i];
                    this.knowledgeElementOptions.push({id:curKE.id, content:curKE.content})
                }                

            },
            // 添加对话框点击了确定事件
            addEventInfo() {
                this.$refs.addFormRef.validate(async valid =>{
                    if (!valid) return
                    // 发起请求
                    console.log(this.editForm)
                    const {data:res} = await this.$http.post("event/insert", this.addForm);
                    if (res.code !== 200) {
                        return this.$message.error("添加失败");
                    }
                    // 关闭对话框
                    this.addDialogVisible = false;
                    // 刷新列表
                    this.getEventList();
                    // 提示更新成功
                    this.$message.success("添加成功");
                } )
            },            
            // 修改对话框点击了确定事件
            editEventInfo() {
                this.$refs.editFormRef.validate(async valid =>{
                    if (!valid) return
                    // 发起请求
                    const {data:res} = await this.$http.post("event/update", this.editForm);
                    if (res.code !== 200) {
                        return this.$message.error("更新失败");
                    }
                    // 关闭对话框
                    this.editDialogVisible = false;
                    // 刷新列表
                    this.getEventList();
                    // 提示更新成功
                    this.$message.success("更新成功");
                } )
            },
            // 刷新事件列表
            async getEventList() {
                const {data:res} = await this.$http.get("event/selectAll", {params: this.queryInfo})
                if (res.code != 200) return this.$message.error("获取事件列表失败!");
                this.eventList = res.data
                for (let i = 0; i < this.eventList.length; i++) {
                    const cur_event = this.eventList[i];

                    var eventObj = new Object();
                    eventObj.text = cur_event.event;
                    eventObj.value = cur_event.event;
                    this.eventFilters.push(eventObj);
                }
                // this.eventList = res.data.eventList;
                // this.total = res.data.total;
            },
            // 表格过滤函数
            filterHandler(value, row, column) {
                const property = column['property'];
                return row[property] === value;
            },
            // 对话框关闭
            addDialogClosed() {
                this.$refs.addFormRef.resetFields();
                this.addForm.addressIdList = [];
                this.addForm.knowledgeElementIdList = [];
                this.addForm.peopleIdList = [];
                this.addForm.timeId = "";
                this.addForm.mapId = "";
            },
            editDialogClosed() {
                this.$refs.editFormRef.resetFields();
                this.editForm.addressIdList = [];
                this.editForm.knowledgeElementIdList = [];
                this.editForm.peopleIdList = [];
                this.editForm.timeId = "";
                this.editForm.mapId = "";
            },
            // 删除操作
            async deleteEventById(id) {
                // 弹框提示
                const conformRes = await this.$confirm('此操作将删除该条, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'  // 图标
                }).catch(err => {
                    return err
                }) 
                // 如果用户确认则返回confirm串 取消返回的是cancel
                // console.log(conformRes)
                if (conformRes !== "confirm") {
                    return this.$message.info("已经取消删除");
                }
                const{data:res} = await this.$http.post("event/deleteById", {id:id});
                if (res.code !== 200) {
                    return this.$message.error(res.meg);
                }
                this.$message.success("删除成功");
                this.getEventList();
            }
        }
    }
</script>

<style lang="less" scoped>
    .box-card {
        line-height: 0%;
        text-align: left;
    }
</style>